
import static org.junit.Assert.*;

import java.security.SecureRandom;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class DeckTest
{
        static int players[] = null;
        Deck testedDeck = null;
        static SecureRandom Generator;

        @BeforeClass
        public static void setUpBeforeClass() throws Exception
        {
                players = new int[]{0,1,2,3,4,5,6};
                Generator = new SecureRandom();
        }

        @AfterClass
        public static void tearDownAfterClass() throws Exception
        {
                players = null;
        }

        @Before
        public void setUp() throws Exception
        {
                testedDeck = new Deck();
        }

        @After
        public void tearDown() throws Exception
        {
                testedDeck = null;
        }
        @Test
        public void checkNULLReference() {
                assertNotNull(this.testedDeck);
        }

        @Test
        public void testShuffle()
        {
                for(int i=0; i<20; i++) {
                        testedDeck.returnCardToDeck(i);
                }
                for(int card=0 ; card<20; card++) {
                        assertEquals(Deck.CARD_RETURNED, testedDeck.getOwnerNumber(card));
                }
                testedDeck.shuffle();
                for(int card = 0; card<52; card++) {
                        assertEquals(Deck.CARD_IN_DECK , testedDeck.getOwnerNumber(card));
                }
        }

        @Test
        public void testGiveCard()
        {
                int cards[] = new int[52];
                for(int i=0; i<52; i++)
                        cards[i] = testedDeck.giveCard(players[Generator.nextInt(6)+1]);
                for(int i=0; i<52; i++) {
                        int owner = testedDeck.getOwnerNumber(i);
                        assertTrue(owner>0 && owner<7);
                }
                testedDeck.returnCardToDeck(5);
                int cardAfterShuffle = testedDeck.giveCard(players[2]);
                if (cardAfterShuffle != 5) fail();
        }

        @Test
        public void testReturnCardToDeck()
        {
                int cards[] = new int[52];
                for(int i=0; i<52; i++)
                        cards[i] = testedDeck.giveCard(players[Generator.nextInt(6)+1]);
                for(int i=0; i<52; i++) {
                        int owner = testedDeck.getOwnerNumber(i);
                        assertTrue(owner>0 && owner<7);
                }
                for(int i=0; i<52; i++) {
                        testedDeck.returnCardToDeck(cards[i]);
                        assertEquals(Deck.CARD_RETURNED, testedDeck.getOwnerNumber(cards[i]));
                }
        }

}